﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Group3.Framework.Extensions
{
    public static class ExtensionsPropertyInfo
    {
        public static bool IsOfType<T>(this object o) where T : class { return typeof(T).Equals(o); }

        public static bool IsMarkedWith<TAttribut>(this PropertyInfo prop) where TAttribut : Attribute
        {
            if (prop == null) return false;
            return
            (from c in prop.GetCustomAttributes(typeof(TAttribut), false)
             select c).FirstOrDefault() != null;
        }
        public static bool IsMarkedWith<TAttribut>(this Type type) where TAttribut : Attribute
        {
            if (type == null) return false;
            return
            (from c in type.GetProperties()
                 from d in c.GetCustomAttributes(typeof(TAttribut), false)
             select d).FirstOrDefault() != null;
        }
        public static TAttribut GetAttribute<TAttribut>(this PropertyInfo prop) where TAttribut : Attribute
        {
            if (prop == null) return null;
            return (from c in prop.GetCustomAttributes(typeof(TAttribut), false)
                    select c).Cast<TAttribut>().FirstOrDefault();

        }
        public static TAttribut GetAttribute<TAttribut>(this Assembly assembly) where TAttribut : Attribute
        {
            if (assembly == null) return null;
            return (from c in assembly.GetCustomAttributes(typeof(TAttribut), false)
                    select c).Cast<TAttribut>().FirstOrDefault();

        }
        public static IEnumerable<TAttribut> GetAttributes<TAttribut>(this PropertyInfo prop) where TAttribut : Attribute
        {
            if (prop == null) return null;
            return (from c in prop.GetCustomAttributes(typeof(TAttribut), false)
                    select c).Cast<TAttribut>();

        }
    }
}
